package com.cloud.code.算法.排序.选择排序;

import java.util.Arrays;

public class SelectSort {

    public static void main(String[] args) {

        Integer[] data = {1, 4, 2, 6, 3};

        selectSort(data);

        selectSortFromLast(data);

        User[] users = {
                new User("xiaoli",32),
                new User("xiaoming",23),
                new User("xiaohua",21)
        };

        selectSort(users);

    }

    private static <E extends Comparable<E>> void selectSort(E[] data) {
        int length = data.length;
        for (int i = 0; i < length; i++) {

            int min = i;

            for (int j = i; j < length; j++) {
                if (data[min] .compareTo( data[j]) > 0) {
                    min = j;
                }
            }
            if (min != i) {
                E tem = data[i];
                data[i] = data[min];
                data[min] = tem;
            }
        }

        System.out.println(Arrays.toString(data));
    }

    private static <E extends Comparable<E>> void selectSortFromLast(E[] data) {
        int length = data.length;
        for (int i = length-1; i >= 0; i--) {

            int max = i;

            for (int j = i; j >=0; j--) {
                if (data[max] .compareTo( data[j]) < 0) {
                    max = j;
                }
            }
            if (max != i) {
                E tem = data[i];
                data[i] = data[max];
                data[max] = tem;
            }
        }

        System.out.println(Arrays.toString(data));
    }
}
